package app.auto.runner.base.utility;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.transition.Slide;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.PopupWindow;

import com.aliang.auto.R;

import app.auto.runner.base.intf.FunCallback;


public class SlideBottomUtil extends PopupWindow {


    public static void demo(){
        Activity context = null;
        View view = null;
        SlideBottomUtil.slideBottomPercent(context,view,0.5f);

    }
    private float layout_height = ViewGroup.LayoutParams.WRAP_CONTENT;

    public SlideBottomUtil setLayoutHeight(float height) {
        this.layout_height = height;
        setHeight((int) layout_height);
        return this;
    }
    public static SlideBottomUtil slideBottomPercent(final Activity activity, final View rview,float percent) {
        int height = new Float(new Float(activity.getWindow().getDecorView().getHeight())*percent).intValue();

        return slideBottom(activity, rview, height);
    }
    public static SlideBottomUtil slideBottom(final Activity activity, final View rview) {
        return slideBottom(activity, rview, -1);
    }

    @SuppressLint("WrongConstant")
    public static SlideBottomUtil slideBottom(final Activity activity, final View rview, float height) {

        final SlideBottomUtil bottomPopupWindow = new SlideBottomUtil(activity, rview).setLayoutHeight(height);
        bottomPopupWindow.setSoftInputMode
                (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN |
                        WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        bottomPopupWindow.setAnimationStyle(R.style.slide_from_bottom);

        return bottomPopupWindow;
    }

    public void show(Activity activity) {

        showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }

    FunCallback funCallback;

    public SlideBottomUtil setFunCallback(FunCallback funCallback) {
        this.funCallback = funCallback;
        return this;
    }

    @SuppressLint("WrongConstant")
    public SlideBottomUtil(final Activity activity, final View res) {
        super(activity);


        res.setBackgroundColor(Color.WHITE);
        //设置SelectPicPopupWindow的View
        this.setContentView(res);
        setBackgroundDrawable(new ColorDrawable(0xffffff));
        int height = activity.getWindow().getDecorView().getHeight();

        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
//        if (layout_height > 0) {
//            layout_height = height * layout_height;
//        }
        if (res.getLayoutParams() != null && res.getLayoutParams().height > 0) {
            setHeight(res.getLayoutParams().height);
        } else if (res.getHeight() > 0) {
            setHeight(res.getHeight());
        }

        PopupWindow window = this;
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);


//        if (dm.heightPixels - dm.widthPixels > 0) {
//
//            //垂直方向，那么切换成水平方向
//            res.findViewById(R.id.pane).getLayoutParams().height = new Float(dm.heightPixels * 0.47f).intValue();//* 21 / 48 / 60 * 72;//1/12//21/48 defination
//        } else {
//            this.setWidth(dm.heightPixels);
//            res.getRootView().findViewById(R.id.part).getLayoutParams().height = dm.heightPixels * 1 / 4;
//            res.findViewById(R.id.pane).getLayoutParams().height = dm.heightPixels * 3 / 4;//1/12//21/48 defination
//        }
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为透明

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                new Handler(Looper.getMainLooper(), new Handler.Callback() {
                    @Override
                    public boolean handleMessage(Message msg) {
                        if (funCallback == null) {

                            WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                            lp.alpha = 1.0f;
                            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                            activity.getWindow().setAttributes(lp);
                        } else {
                            funCallback.onCallback(null, null);
                        }
                        return false;
                    }
                }).sendEmptyMessage(0);
            }
        });

    }

}
